package com.xuanyang.dormrepair.controller;

import com.xuanyang.dormrepair.config.hook.AdminLogin;
import com.xuanyang.dormrepair.config.hook.OpenApi;
import com.xuanyang.dormrepair.utils.Const;
import com.xuanyang.dormrepair.utils.RestResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @AUTHOR soft
 * @DATE 2019/5/19 0:46
 * @DESCRIBE
 */
@Controller
public class ViewController {

    @AdminLogin
    @GetMapping({"/", "/admin", "/admin/home"})
    public String home() {
        return "admin/home";
    }

    @AdminLogin
    @GetMapping("/admin/{path}")
    public String view(@PathVariable String path) {
        return "admin/" + path;
    }

    /**
     * 管理员登录界面
     */
    @OpenApi
    @GetMapping("/login")
    public String login() {
        return "admin/login";
    }

    /**
     * 管理员注销
     */
    @OpenApi
    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute(Const.USER);
        return "redirect:/login";
    }

    @OpenApi
    @RequestMapping("/api/nologin")
    @ResponseBody
    public RestResponse nologin() {
        return RestResponse.fail(401, "需要登录！");
    }
}
